<?php

/**
 * cottonOrderGoodsItem actions.
 *
 * @package    WacStorehouse
 * @subpackage cottonOrderGoodsItem
 * @author     JianBinBi <prince.bi@gmail.com>
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class cottonOrderGoodsItemActions extends WacCommonActions
{
     /**
     * get searching list
     * @package
     * @author     ben
     */
    public function executeGetList(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $jqGridDataHelper = JqGridDataHelper::getInstance();
        $arrParam = array();

        if($jqGridDataHelper->loadComplexQuery($request)) {            

            if($request->hasParameter(JqGridDataHelper::$KEY_SEARCH_FIELD) && $request->getParameter(JqGridDataHelper::$KEY_SEARCH_OPER)!="") {
                $arrParam['andWhere'] = array(
                        $jqGridDataHelper->getCondMapStr($request->getParameter(JqGridDataHelper::$KEY_SEARCH_OPER), "t1.".$request->getParameter(JqGridDataHelper::$KEY_SEARCH_FIELD), $request->getParameter(JqGridDataHelper::$KEY_SEARCH_STRING))
                );
            }

            if($request->hasParameter(JqGridDataHelper::$KEY_SIDX)) {
                switch($request->getParameter(JqGridDataHelper::$KEY_SIDX)) {
                    case "cotton_order_code":
                        $arrParam['orderBy'] = "wco.code ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                    case "standard_name":
                        $arrParam['orderBy'] = "wstd.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                    case "factory_name":
                        $arrParam['orderBy'] = "wf.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                    case "status_name":
                        $arrParam['orderBy'] = "t1.status ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
//                    case "goods_category_name":
//                        $arrParam['orderBy'] = "wgc.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
//                        break;
                    case "goods_name":
                        $arrParam['orderBy'] = "wg.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                    default:
                        $arrParam['orderBy'] = "t1.".$request->getParameter(JqGridDataHelper::$KEY_SIDX)." ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                }
            }

            $resultSet = $this->mainModuleTable->getCustomList($arrParam, $request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false);
        }
        else {
            if($request->hasParameter(JqGridDataHelper::$KEY_SEARCH_FIELD) && $request->getParameter(JqGridDataHelper::$KEY_SEARCH_FIELD)=="parent_id") {
                $arrParam['andWhere'][] = "cotton_order_id=".$this->getRequest()->getParameter(JqGridDataHelper::$KEY_SEARCH_STRING);
            }
            $resultSet = $this->mainModuleTable->getCommonList($request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false, $arrParam);
        }

        $filterResultSet = $this->filterList($resultSet);

        $pager = $this->mainModuleTable->getPager();
        $filterResultSet = $jqGridDataHelper->convert($filterResultSet, $pager, JsCommonData::getSuccessDatum());

//        echo "cccccc: ".JqGridDataHelper::getInstance()->loadComplexQuery($request);
//        $filterResultSet = $request->getParameterHolder()->getAll();
        return OutputHelper::getInstance()->outputJsonOrTextFormat($filterResultSet, $this);
    }

    /*
   * override filter list
    */
    public function filterList($listObjs) {
        $filterArr = array();
        if(count($listObjs) > 0) {
            foreach($listObjs as $listObj) {
                $tmpArr = $listObj->toArray();
                $tmpArr['production_order_code']        = $listObj->getWacProductionOrder()->getCode();
                $tmpArr['cotton_order_code']            = $listObj->getWacCottonOrder()->getCode();
                $tmpArr['goods_number_unit_code_name']  = $listObj->getWacGoodsNumberUnitCode()->getName();
                $tmpArr['standard_name']                = $listObj->getWacStandard()->getName();
                $tmpArr['factory_name']                 = $listObj->getWacFactory()->getName();
                $tmpArr['goods_category_name']          = $listObj->getWacGoodsCategory()->getName();
                $tmpArr['goods_name']                   = $listObj->getWacGoods()->getName();

                $filterArr[] = $tmpArr;
            }
        }

        return $filterArr;
    }

    /*
   * @return inspect result
    */
    public function inspectDataValidation(sfWebRequest $request, $params=array()) {
        $result    = JsCommonData::getSuccessDatum();
        $reqParams = $request->getParameterHolder()->getAll();

//        if(empty($reqParams['goods_category_id']) || !Doctrine::getTable(WacTable::$goodsCategory)->isExistedId($reqParams['goods_category_id'])) {
//            $result = JsCommonData::getErrorDatum(
//                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_goods_category"),  WacErrorCode::$logicError
//            );
//            return $result;
//        }

        if(empty($reqParams['cotton_order_id']) || !Doctrine::getTable(WacTable::$cottonOrder)->isExistedId($reqParams['cotton_order_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_cotton_order"),  WacErrorCode::$logicError
            );
            return $result;
        }

//        if(empty($reqParams['factory_id']) || !Doctrine::getTable(WacTable::$factory)->isExistedId($reqParams['factory_id'])) {
//            $result = JsCommonData::getErrorDatum(
//                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_factory"),  WacErrorCode::$logicError
//            );
//            return $result;
//        }

        return $result;
    }

    /*
   * @return list data array
    */
    public function executeAdd(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);

        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet['info'] = $inspectResult;
        }
        else {
            $objCottonOrder = Doctrine::getTable(WacTable::$cottonOrder)->findOneBy('id', $reqParams['cotton_order_id']);
            $targetItem      = $this->mainModuleTable->create();

            if(count($reqParams)>0) {
//              $targetItem->set($reqParams['']);

                $targetItem->setProductionOrderId($objCottonOrder->getProductionOrderId());
                $targetItem->setSrcProductionOrderId($objCottonOrder->getProductionOrderId());
                $targetItem->setCottonOrderId($objCottonOrder->getId());
                $targetItem->setFactoryId($objCottonOrder->getFactoryId());

                if(isset($reqParams['goods_id']) && isset($reqParams['goods_name'])) {
                    $tmpParams = array(
                          "id" => $reqParams['goods_id'],
                          "name" => $reqParams['goods_name']
                        ) ;
                    $objGoods = Doctrine::getTable(WacTable::$goods)->register($tmpParams, $objCottonOrder);
                    $targetItem->setGoodsId($objGoods->getId());
                    $targetItem->setGoodsCategoryId($objGoods->getGoodsCategoryId());
                }
//                if(isset($reqParams['batch'])) {
//                    $targetItem->setBatch($reqParams['batch']);
//                }
                if(isset($reqParams['standard_id'])) {
                    $targetItem->setStandardId($reqParams['standard_id']);
                }
                if(isset($reqParams['goods_number'])) {
                    $targetItem->setGoodsNumber($reqParams['goods_number']);
                }
                if(isset($reqParams['goods_number_unit_code'])) {
                    $targetItem->setGoodsNumberUnitCode($reqParams['goods_number_unit_code']);
                }
                if(isset($reqParams['goods_number_unit_price'])) {
                    $targetItem->setGoodsNumberUnitPrice($reqParams['goods_number_unit_price']);
                    $targetItem->setCost($reqParams['goods_number_unit_price'] * $reqParams['goods_number']);
                    $targetItem->setCurrencyCode($objCottonOrder->getCurrencyCode());
                }
                if(isset($reqParams['memo'])) {
                    $targetItem->setMemo($reqParams['memo']);
                }

                $targetItem->save();

                $this->afterAdd($request, $targetItem);

            }

            $resultSet['items'][0] = $targetItem->toArray();
            $resultSet['info']     = JsCommonData::getSuccessDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_add_succ")
            );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
   * @return list data array
    */
    public function executeEdit(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }
        
        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);

        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet['info'] = $inspectResult;
        }
        else {
            $targetItem = $this->mainModuleTable->findOneById($request->getParameter('id'));

            if(count($reqParams)>0) {
                if(isset($reqParams['goods_id']) && isset($reqParams['goods_name'])) {
                    $tmpParams = array(
                          "id" => $reqParams['goods_id'],
                          "name" => $reqParams['goods_name']
                        ) ;
                    $objGoods = Doctrine::getTable(WacTable::$goods)->register($tmpParams, $targetItem->getWacCottonOrder());
                    $targetItem->setGoodsId($objGoods->getId());
                    $targetItem->setGoodsCategoryId($objGoods->getGoodsCategoryId());
                }
//                if(isset($reqParams['batch'])) {
//                    $targetItem->setBatch($reqParams['batch']);
//                }
                if(isset($reqParams['standard_id'])) {
                    $targetItem->setStandardId($reqParams['standard_id']);
                }
                if(isset($reqParams['goods_number'])) {
                    $targetItem->setGoodsNumber($reqParams['goods_number']);
                }
                if(isset($reqParams['goods_number_unit_code'])) {
                    $targetItem->setGoodsNumberUnitCode($reqParams['goods_number_unit_code']);
                }
                if(isset($reqParams['goods_number_unit_price'])) {
                    $targetItem->setGoodsNumberUnitPrice($reqParams['goods_number_unit_price']);
                    $targetItem->setCost($reqParams['goods_number_unit_price'] * $reqParams['goods_number']);
                }
                if(isset($reqParams['memo'])) {
                    $targetItem->setMemo($reqParams['memo']);
                }

                $targetItem->save();

                $this->afterEdit($request, $targetItem);

            }

            $resultSet['items'][0] = $targetItem->toArray();
            $resultSet['info']     = JsCommonData::getSuccessDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_edit_succ")
            );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
   * @return list data array
    */
    public function executeGetFormData(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = JsCommonData::getCommonDatum();
        $resultSet['info'] = JsCommonData::getSuccessDatum();

//        $resultSet['items']['factory']        = Doctrine::getTable(WacTable::$factory)->getIdNameHash();
        $resultSet['items']['standard']       = Doctrine::getTable(WacTable::$standard)->getIdNameHash();
        $resultSet['items']['unit']           = Doctrine::getTable(WacTable::$unit)->getCodeNameHash();

//        $resultSet['items']['default']['currency_code_name'] = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$currency);
        $resultSet['items']['default']['cotton_unit_code'] = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$conttonUnitCode);



        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }
}